﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class SmsBLL
    {
        public const string DELEGATE = "WT";
        public const string CANCEL = "QX";
        public const string QUERY = "CX";

        public const string SMSCENTERNUMBER = "短信平台"; //短信平台号码

        public enum SmsType
        {
            Notice = 0,        //通知
            Ignore = 1,        //忽略    
            Reply = 2,         //回复
            Delegate = 3,      //委托
            Cancel = 4,        //取消
            Query = 5,         //查询
            Erro = 6           //异常
        }

        /// <summary>
        /// 根据接受到的短信判断业务类型
        /// </summary>
        /// <param name="sms"></param>
        /// <returns></returns>
        public string SMSResolve(string sms)
        {
            string SMSHead;
            SMSHead = sms.Substring(0, 2);
            return SMSHead.ToUpper();

        }

        /// <summary>
        /// 根据手机号码检查选民是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool CheckXmByPhone(string phone)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm xm = dc.Xm.FirstOrDefault(x => x.sjhm == phone);
                if (xm == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        /// <summary>
        /// 根据短信来源手机号码获得选民
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public DAL.Xm GetXMByPhone(string phone)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm xm = dc.Xm.FirstOrDefault(x => x.sjhm == phone);
                return xm;
            }
        }

        /// <summary>
        /// 判断短信来源手机号码是否为登记号码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool CheckPhone(string phone)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm xm = dc.Xm.FirstOrDefault(x => x.sjhm == phone);
                return xm != null;
            }
        }


        /// <summary>
        /// 保存短信
        /// </summary>
        /// <param name="xjbh">选举编号</param>
        /// <param name="flag">短信标记</param>
        /// <param name="strMessage">短信内容</param>
        /// <param name="phone">目标手机号码</param>
        /// /// <param name="flag">本机号码</param>
        public void SaveSms(string xjbh, SmsType flag, string strMessage, string mbPhone, string bjPhone)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Dxjl dx = new DAL.Dxjl();
                dx.xjbh = xjbh;
                dx.nr = strMessage;
                dx.flag = ((int)flag).ToString();
                dx.fssj = DateTime.Now;
                dx.mbsjhm = mbPhone;
                dx.bjhm = bjPhone;

                dc.Dxjl.InsertOnSubmit(dx);
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 保存短信
        /// </summary>
        /// <param name="xjbh">选举编号</param>
        /// <param name="flag">短信标记</param>
        /// <param name="strMessage">短信内容</param>
        /// <param name="mbPhone">目标手机号码</param>
        /// <param name="bjPhone">本机号码</param>
        /// <param name="time">通讯时间</param>
        public void SaveSms(string xjbh, SmsType flag, string strMessage, string mbPhone, string bjPhone, DateTime time)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Dxjl dx = new DAL.Dxjl();
                dx.xjbh = xjbh;
                dx.nr = strMessage;
                dx.flag = ((int)flag).ToString();
                dx.fssj = time;
                dx.mbsjhm = mbPhone;
                dx.bjhm = bjPhone;

                dc.Dxjl.InsertOnSubmit(dx);
                dc.SubmitChanges();
            }
        }
    }
}
